package com.mijie.query;


import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.mijie.fentsi.renvaEventC;
import com.mijie.homi.search.util.DbNdsController;
import com.mijie.homi.search.util.god;
import com.mijie.homi.search.util.js;
import com.mijie.homi.search.util.listUtil;

import net.sf.json.JSONArray;

public class fulltxtControllor4invoke extends fulltxtCtrlMultiThrd {
	public fulltxtControllor4invoke() {
	}

	public static void main(String[] args) {
		fulltxtControllor4invoke c = new fulltxtControllor4invoke();
		c.writeToIndexSinglePerMessage("170203", "sc");
		System.out.println("f");
		
		
		
	//	writeToIndexSinglePerMessageTest();
	}

	
	private static void writeToIndexSinglePerMessageTest() {
		fulltxtControllor.ini_fentsiControllerX();
		String s="纠结@我爱上班上的一个女生";
	 	
		String str = "我爱上班上的一个女生";
		List<String> txtFentsied = fulltxtControllor.MMsegFentsicyi
		.fentsi(str);
		Map m=new HashMap<String, Object>();
		m.put("id", "");
		fulltxtControllor4invoke c = new fulltxtControllor4invoke();
		c.from="sincin";
		c.Cur_str=str;
		c.writeToIndexSinglePerMessage( m,txtFentsied);
		//writeToIndexSinglePerMessage
	}

	/**
	 * 
	 * @param msgId
	 * @param scOrPr
	 */
	public void writeToIndexSinglePerMessage(String msgId, String scOrPr) {
		fulltxtControllor.ini_fentsiControllerX();
		String Sql;
		Map map;
		String sincin = "sc";
		if (scOrPr.equals(sincinController.sincin)) {
			Sql = "select  * from mood where  id=" + msgId;
			dbc = new DbNdsController();
			ResultSet rs = dbc.getrs(Sql);
			map = this.converToMap(rs);
			
			//o1e  change map moodid>>>moodname
		//	for(map.)
			
			this.from = "sincinlib";
		} else {
			this.from = "problemlib";
			Sql = "select    *   from topic where id= " + msgId;
			dbc = new DbNdsController();
			ResultSet rs = dbc.getrs(Sql);
			map = this.converToMap4prblm(rs);

		}
		String line = (String) map.get("content");
		List<String> txtFentsied = fulltxtControllor.MMsegFentsicyi
				.fentsi(line);

		// this.curLIneFentsi_word_arr=txtFentsied;
		this.Cur_str = line;
		if(	this.indexBatchID==null)
		this.indexBatchID = "cb23";
		writeToIndexSinglePerMessage( map, txtFentsied);
	}
	public static Logger logger_autoIndex ;//nbt
	/**
	 * @author attilax 1466519819@qq.com
	 * @param mp
	 * @param curLIneFentsi_word_arr
	 *            , Cur_str
	 */
	private void writeToIndexSinglePerMessage(Map mp,
			List<String> curLIneFentsi_word_arr) {

		// cb9
		// this.dbc=new DbNdsController();

		// //c28 e

		System.out.println("正在建立索引 : " + curLIneFentsi_word_arr + "");
		this.id = (String) mp.get("id");

		String[] txtFentsiedArray = curLIneFentsi_word_arr
				.toArray(new String[curLIneFentsi_word_arr.size()]);
		// pilyeo hteu zaozao set le .
		if (renvaEventCca28 == null)
			renvaEventCca28 = new renvaEventC();

		// get renva
		if (this.from.equals("problemlib")) {
			//String renvaids = (String) mp.get("renvaids");

			//nge heutai shha d renva fenlei 
		//	this.renvaList = renvaEventCca28.geneRenvaList4problemlib(
		//			renvaids, mp);
			// this.renvaAll = JSONArray.fromObject(renvaResult).toString();
			// get type  o12
			this.renvaList  = renvaEventCca28.getRenvasByKWnDweiuNKW2(
					txtFentsiedArray, this.Cur_str);
		} else {

			//o17
			List<Map> renvaResult =renvaEventCca28.getRenvasByKWnDweiuNKW2(txtFentsiedArray, this.Cur_str);
			this.renvaList =renvaResult;
			// this.renva = getrenva(txtFentsied);

		}
		this.renvaAll = JSONArray.fromObject(this.renvaList).toString();

		// get event
		renvaEventC rc = renvaEventCca22;
		@SuppressWarnings("unused")
		// List liEvent = renvaEventCca28.getEventsFromDBlib(txtFentsiedArray,
		// this.renvaList,this.txtLine);
		// cb5 change
		List liEvent = renvaEventCca28.getEventsFromOneRenvaRelation(
				txtFentsiedArray, "", this.Cur_str);
		this.events_li = liEvent;
		this.eventAll = JSONArray.fromObject(liEvent).toString();
		this.liEvents = liEvent;

		// //cinsyv
		this.mid = (String) mp.get("id");
		if (this.mid.equals("23763"))
			System.out.println("a");
		String moodnamej = (String) mp.get("moodname");
		moodnamej = convetMoodName2cinsyv_habin(moodnamej);
		String[] mood_arr = moodnamej.split(",");
		
		
		if(logger_autoIndex!=null)
			logger_autoIndex.info("start to write sql to index ");
		dbc = new DbNdsController();
		for (String mood : mood_arr) {
			this.cinsyv = getcinsyv(mood);

			System.out.println("--cinsyv" + this.cinsyv);
			System.out.println("--event" + this.event);
			String cur_splitBySpace = listUtil.toString(
					curLIneFentsi_word_arr, " ");
			this.uid = (String) mp.get("userId");
			this.moodid = (String) mp.get("moodid");

			this.moodname = (String) mp.get("moodname");
			// this.curLinenum=i+1;
			// TOD debug no db
			List<String> sqls = geneSqls_part(cur_splitBySpace);

		}
		// okkk();

		// save jindu
		isAddToSqlqueue = false; // cb23
		savejindu(this.mid, this.from);
		
		
	
		dbc.execCb2(saveJindu_sql);
		this.dbc.close();

		try {
			if (1 == 2)
				Thread.sleep(1);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}

	}

	/**
	 * renva event sincin trave
	 */
	protected List<String> geneSqls_part(String txtFentsied) {

		List<String> sqls_li = new ArrayList<String>();
		int n = 0;
		for (Map mp : this.renvaList) {
			for (Map mp_event : this.events_li) {
				String renva = (String) mp.get("name");

				// Map mpEvent = (Map) this.liEvents.get(n);
				Map mpEvent = mp_event;
				String eventx = (String) mpEvent.get("event");

				String mood4fullTable_now; // cb0
				if (mood4fullTable == null)
					mood4fullTable_now = "mood4full";
				else
					mood4fullTable_now = mood4fullTable;
				String sql = "	insert into "
						+ mood4fullTable_now
						+ "(mid,content,renva,"
						+ "event,cinsyv,renvaAll,eventAll,fromx,createAt)"
						+ "values(#{id}, #{content},@renva, @event,@cinsyv,@renvaAll,@eventAll,@from,now())";

				// sql=sql.replaceAll("#", this.id);
				sql = sql.replaceAll("#\\{id\\}", this.id);
				sql = sql
						.replaceAll("#\\{content\\}", "'" + txtFentsied + "'");
				String convetSqlStringFromatx = convetSqlStringFromat(this.renvaAll);
				sql = sql.replaceAll("@renvaAll", convetSqlStringFromatx);
				sql = sql.replaceAll("@eventAll",
						convetSqlStringFromat(this.eventAll));

				sql = sql.replaceAll("@renva", convetSqlStringFromat(renva));
				sql = sql.replaceAll("@event", convetSqlStringFromat(eventx));
				sql = sql.replaceAll("@cinsyv",
						convetSqlStringFromat(this.cinsyv));
				sql = sql
						.replaceAll("@from", convetSqlStringFromat(this.from));

				// System.out.println(sql);
				sqls_li.add(sql);
				logger.info(sql);
				dbc.execCb2(sql);
				// List<String>
				// return ds;

				// n++;
			}
		}
		return sqls_li;
	}

}
